﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.ClientCard;
using INSE6260.OnlineBanking.Model.Clients;
using INSE6260.OnlineBanking.Web.MVC.Helpers;

namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class ProfileController : Controller
    {
        private readonly IClientRepository _clientRepository;
        private readonly IClientCardRepository _clientCardRepository;

        public ProfileController(IClientRepository clientRepository, IClientCardRepository clientCardRepository)
        {
            _clientRepository = clientRepository;
            _clientCardRepository = clientCardRepository;
        }

        #region UpdateProfile
        public ActionResult UpdateProfile()
        {
            if (ModelState.IsValid)
            {
                var client = _clientRepository.GetClientByClientId(UserProvider.ActiveClient.ClientId);
                return View("UpdateProfile", client);
            }
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateProfile(Client client)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var dbClient = _clientRepository.FindClient(UserProvider.ActiveClient.ClientId);
                    if (dbClient != null)
                    {
                        dbClient.Address = client.Address;
                        dbClient.Telephone = client.Telephone;
                        dbClient.Email = client.Email;

                        _clientRepository.UpdateProfile(dbClient);
                        ViewBag.Message = "The Information is updated seccessfully";
                        return View("ShowProfile",dbClient);
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("InvalidMessage", e.Message);
                }
            }

            return View(client);
        }
        #endregion

        #region Show Profile
        public ActionResult ShowProfile()
        {
            if (ModelState.IsValid)
            {
                var client = _clientRepository.GetClientByClientId(UserProvider.ActiveClient.ClientId);
                return View(client);
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ShowProfile(Client client)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var dbClient = _clientRepository.FindClient(UserProvider.ActiveClient.ClientId);
                    if (dbClient != null)
                    {
                        return View("UpdateProfile", dbClient);
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("InvalidMessage", e.Message);
                }
            }

            return View(client);
        }
        #endregion

        #region ChangePassword

        public ActionResult ChangePassword()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangePassword(PasswordInfo passwordInfo)
        {
            if (ModelState.IsValid)
            {
                if (passwordInfo.OldPassword == UserProvider.ActiveClient.Password)
                {
                    if (passwordInfo.NewPassword == passwordInfo.ConfirmPassword)
                    {
                        try
                        {
                            var clientCard = _clientCardRepository.GetClientCard(UserProvider.ActiveClient.ClientCardId);
                            clientCard.Password = passwordInfo.NewPassword;
                            _clientCardRepository.ChangePassword(clientCard);
                            UserProvider.ActiveClient.Password = clientCard.Password;
                            ViewBag.ValidMessage = "The password is changed successfully";
                            return View("ChangePassword");
                        }
                        catch (Exception e)
                        {

                        }
                    }
                    ViewBag.InvalidMessage = "The new password and the confirmed password are not the same";
                    return View("ChangePassword");
                }
                ViewBag.InvalidMessage = "The current password is not correct";
                return View("ChangePassword");
            }
            return View("ChangePassword");
        }

        #endregion

        #region ChangeSecurityQuestion
        public ActionResult ChangeSecurityQuestion()
        {
            var listQuestion = MakeListQuestion();


            var clientCard = _clientCardRepository.GetClientCard(UserProvider.ActiveClient.CardNo, UserProvider.ActiveClient.Password);

            var securityQuestionInfo = new SecurityQuestionInfo();
            foreach (var question in listQuestion)
            {
                if (question.Name == clientCard.SecurityQuestion1)
                {
                    var selectList1 = new SelectList(listQuestion, "Id", "Name", question.Id);
                    ViewData["Question1"] = selectList1;
                    securityQuestionInfo.Answer1 = clientCard.SecurityAnswer1;
                    securityQuestionInfo.Question1 = clientCard.SecurityQuestion1;
                }
                if (question.Name == clientCard.SecurityQuestion2)
                {
                    var selectList2 = new SelectList(listQuestion, "Id", "Name", question.Id);
                    ViewData["Question2"] = selectList2;
                    securityQuestionInfo.Answer2 = clientCard.SecurityAnswer2;
                    securityQuestionInfo.Question2 = clientCard.SecurityQuestion2;
                }
                if (question.Name == clientCard.SecurityQuestion3)
                {
                    var selectList3 = new SelectList(listQuestion, "Id", "Name", question.Id);
                    ViewData["Question3"] = selectList3;
                    securityQuestionInfo.Answer3 = clientCard.SecurityAnswer3;
                    securityQuestionInfo.Question3 = clientCard.SecurityQuestion3;
                }
            }

            return View("ChangeSecurityQuestion", securityQuestionInfo);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChangeSecurityQuestion(SecurityQuestionInfo securityQuestionInfo)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var clientCard = _clientCardRepository.GetClientCard(UserProvider.ActiveClient.ClientCardId);
                    clientCard.SecurityAnswer1 = securityQuestionInfo.Answer1;
                    clientCard.SecurityAnswer2 = securityQuestionInfo.Answer2;
                    clientCard.SecurityAnswer3 = securityQuestionInfo.Answer3;
                    clientCard.SecurityQuestion1 = securityQuestionInfo.Question1;
                    clientCard.SecurityQuestion2 = securityQuestionInfo.Question2;
                    clientCard.SecurityQuestion3 = securityQuestionInfo.Question3;
                    _clientCardRepository.ChangeSecurityQuestions(clientCard);

                    ViewBag.ValidMessage = "The process is done successfully";
                    return RedirectToAction("UpdateProfile");
                }
                catch (Exception e)
                {

                }
            }
            var listQuestion = MakeListQuestion();
            ViewData["Question3"] = new SelectList(listQuestion, "Id", "Name", securityQuestionInfo.Question3);
            ViewData["Question2"] = new SelectList(listQuestion, "Id", "Name", securityQuestionInfo.Question2); ;
            ViewData["Question1"] = new SelectList(listQuestion, "Id", "Name", securityQuestionInfo.Question1); ;
            return View("ChangeSecurityQuestion", securityQuestionInfo);
        }

        #endregion

        #region Private Method
        private List<Question> MakeListQuestion()
        {
            var listQuestion =
                new List<Question>
                    {
                        new Question() {Id = "", Name = "Name of your oldest sibling"},
                        new Question {Id = "", Name = "Name of your first pet"},
                        new Question {Id = "", Name = "Your first employer"},
                        new Question {Id = "", Name = "Name of your first teacher"},
                        new Question {Id = "", Name = "Your favorite food"},
                        new Question {Id = "", Name = "Name of the city you were born in"},
                        new Question {Id = "", Name = "Your favorite beverage"},
                        new Question {Id = "", Name = "Your first car"},
                        new Question {Id = "", Name = "Your best friend's name"}
                    };
            listQuestion.ForEach(q => q.Id = q.Name);
            ViewBag.Question1 = listQuestion;
            ViewBag.Question2 = listQuestion;
            ViewBag.Question3 = listQuestion;
            return listQuestion;
        }
        #endregion
    }
}
